﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GtsServer.Model;
using GtsServer.Common;
using GtsServer.Manage.Controller;

namespace GtsServer.Manage.IISPage.Ajax
{
    /// <summary>
    /// CommunicationAjax 的摘要说明
    /// </summary>
    public class CommunicationAjax : IHttpHandler
    {

        CommunicationController Controller = new CommunicationController();
        public void ProcessRequest(HttpContext context)
        {
            string type = string.Empty;
            if (context.Request["action"] != null)
            {
                type = context.Request["action"];

                switch (type)
                {
                    case "GetListJson":
                        GetListJson(context);
                        break;
                    case "GetList":
                        GetList(context);
                        break;
                    case "GetListByPID"://根据PID来查询
                        GetListByPID(context);
                        break;
                    case "GetListByID"://推送调用
                        GetListByID(context);
                        break;
                    case "Add":
                        Add(context);
                        break;
                    case "Update":
                        Update(context);
                        break;
                    case "Delete":
                        Delete(context);
                        break;
                    default:
                        break;
                }
            }
        }

        private void GetListJson(HttpContext context)
        {
            List<Communication> list = Controller.GetList();
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Remark = Microsoft.JScript.GlobalObject.encodeURIComponent(list[i].Remark);
            }
            string json = JsonHelper.ListToJson<Communication>(list);
            context.Response.Write(json);
        }

        private void GetList(HttpContext context)
        {
            int pageIndex = Convert.ToInt32(context.Request["pageIndex"]);
            int pageSize = Convert.ToInt32(context.Request["pageSize"]);
            int count = 0;
            List<Communication> list = Controller.GetList(pageIndex, pageSize, out count);
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Remark = Microsoft.JScript.GlobalObject.encodeURIComponent(list[i].Remark);
            }
            string json = JsonHelper.ListToJson<Communication>(list, count.ToString());
            context.Response.Write(json);
        }
        /// <summary>
        /// 根据PID来查询
        /// </summary>
        /// <param name="context"></param>
        private void GetListByPID(HttpContext context)
        {
            List<Communication> list = Controller.GetListByID();
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Remark = Microsoft.JScript.GlobalObject.encodeURIComponent(list[i].Remark);
            }
            string json = JsonHelper.ListToJson<Communication>(list);
            context.Response.Write(json);
        }
        //推送调用
        private void GetListByID(HttpContext context)
        {
            List<Communication> list = Controller.GetListByID();
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Remark = Microsoft.JScript.GlobalObject.encodeURIComponent(list[i].Remark);
            }
            string json = JsonHelper.ListToJson<Communication>(list);
            context.Response.Write(json);
        }
        private void Add(HttpContext context)
        {
            string name = context.Request["name"];
            string pid = context.Request["Pid"];
            string Remark = context.Request["Remark"];
            bool objectIsUsed = Convert.ToBoolean(context.Request["IsUsed"].ToString());
            string pwd = context.Request["pwd"];
            double pagingPrencent = Convert.ToDouble(context.Request["pagingPrencent"].ToString());
            DateTime createTime = Convert.ToDateTime(DateTime.Now.ToString());
            Communication model = new Communication()
            {
                IsUsed = objectIsUsed,
                Name = name,
                Pid = pid,
                Remark = Remark,
                CreateTime = createTime,
                Pwd = pwd,
                PagingPrencent = pagingPrencent
            };
            int id = Controller.Add(model);
            context.Response.Write(id);
        }
        private void Delete(HttpContext context)
        {
            int id = Int32.Parse(context.Request["name"]);
            context.Response.Write(Controller.Delete(id));
        }
        private void Update(HttpContext context)
        {
            int id = Int32.Parse(context.Request["id"].ToString());
            string name = context.Request["name"];
            string pid = context.Request["Pid"];
            string Remark = context.Request["Remark"];
            string pwd = context.Request["pwd"];
            bool objectIsUsed = Convert.ToBoolean(context.Request["IsUsed"].ToString());
            double pagingPrencent = Convert.ToDouble(context.Request["pagingPrencent"].ToString());

            Communication model = new Communication()
            {
                ID = id,
                IsUsed = objectIsUsed,
                Name = name,
                Pid = pid,
                Remark = Remark,
                Pwd = pwd,
                PagingPrencent = pagingPrencent
            };
            context.Response.Write(Controller.Update(model));
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}